﻿using System;
using System.Configuration;
using System.Diagnostics;

namespace DllShepherd.Core.Esri
{
    /// <summary>
    /// Used when there is only one SDE connection needed
    /// </summary>
    public class DefaultDbConfigSection : ConfigurationSection
    {
        private const string DbConnectionTemplate = "Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3}";
        private const string SdeConnectionTemplate = "SERVER={0};INSTANCE=sde:{4}:{0};USER={2};PASSWORD={3};Database={1};VERSION=SDE.Default";

        private const string ConfigSectionName = "DefaultDbConfigSection";

        private string _dbConnectionString = null;
        private string _sdeConnectionString = null;

        #region GetSection Pattern

        private static DefaultDbConfigSection _configSection;

        /// <summary>
        /// Gets the configuration section using the default element name.
        /// </summary>
        public static DefaultDbConfigSection Instance
        {
            [DebuggerStepThrough]
            get
            {
                if (_configSection == null)
                {
                    _configSection = GetSection();
                }
                return _configSection;
            }
        }

        /// <summary>
        /// Gets the configuration section using the specified element name.
        /// </summary>
        /// <exception cref="ConfigurationException">When the section is not defined in the config file</exception>
        public static DefaultDbConfigSection GetSection()
        {
            if (_configSection == null)
            {
                _configSection = ConfigurationManager.GetSection(ConfigSectionName) as
                            DefaultDbConfigSection;
                if (_configSection == null)
                    throw new ConfigurationErrorsException(String.Format("The <{0}> section is not defined in your .config file!", ConfigSectionName));
            }

            return _configSection;
        }

        public static bool Exists()
        {
            return (_configSection != null || ConfigurationManager.GetSection(ConfigSectionName) != null);
        }

        #endregion GetSection Pattern

        [ConfigurationProperty("ServerType", DefaultValue = "sqlserver", IsRequired = false)]
        protected string ServerType
        {
            get
            {
                return (string)this["ServerType"];
            }
            set
            {
                this["ServerType"] = value;
            }
        }

        [ConfigurationProperty("ServerName", DefaultValue = "", IsRequired = true)]
        protected string ServerName
        {
            get
            {
                return (string)this["ServerName"];
            }
            set
            {
                this["ServerName"] = value;
            }
        }

        [ConfigurationProperty("DatabaseCatalogName", DefaultValue = "", IsRequired = true)]
        protected string DatabaseCatalogName
        {
            get
            {
                return (string)this["DatabaseCatalogName"];
            }
            set
            {
                this["DatabaseCatalogName"] = value;
            }
        }

        [ConfigurationProperty("UserName", DefaultValue = "", IsRequired = true)]
        protected string UserName
        {
            get
            {
                return (string)this["UserName"];
            }
            set
            {
                this["UserName"] = value;
            }
        }

        [ConfigurationProperty("Password", DefaultValue = "", IsRequired = true)]
        protected string Password
        {
            get
            {
                return (string)this["Password"];
            }
            set
            {
                this["Password"] = value;
            }
        }

        public string DbConnectionString
        {
            get
            {
                if (_dbConnectionString == null)
                    _dbConnectionString = String.Format(DbConnectionTemplate, ServerName, DatabaseCatalogName, UserName, Password);
                return _dbConnectionString;
            }
        }

        public string SdeConnectionString
        {
            get
            {
                if (_sdeConnectionString == null)
                    _sdeConnectionString = String.Format(SdeConnectionTemplate, ServerName, DatabaseCatalogName, UserName, Password, ServerType);
                return _sdeConnectionString;
            }
        }
    }
}
